package com.example.tripartite.jsonpath;

import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.ReadContext;
import lombok.Data;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author: Bin.L
 * @date: 2022/3/12 15:01
 * @Description: A JsonPath for example demo [https://www.cnblogs.com/wynjauu/articles/9556396.html]
 */
public class JsonPathExample {

    /*{
        "store": {
        "book": [{
            "category": "reference",
                    "author": "Nigel Rees",
                    "title": "Sayings of the Century",
                    "price": 8.95
        }, {
            "category": "fiction",
                    "author": "Evelyn Waugh",
                    "title": "Sword of Honour",
                    "price": 12.99
        }, {
            "category": "fiction",
                    "author": "Herman Melville",
                    "title": "Moby Dick",
                    "isbn": "0-553-21311-3",
                    "price": 8.99
        }, {
            "category": "fiction",
                    "author": "J. R. R. Tolkien",
                    "title": "The Lord of the Rings",
                    "isbn": "0-395-19395-8",
                    "price": 22.99
        }],
        "bicycle": {
            "color": "red",
                    "price": 19.95
        }
    },
        "expensive": 10
    }*/

    public static void main(String[] args) {

        String jsonString = "{\"store\":{\"book\":[{\"category\":\"reference\",\"author\":\"Nigel Rees\",\"title\":\"Sayings of the Century\",\"price\":8.95},{\"category\":\"fiction\",\"author\":\"Evelyn Waugh\",\"title\":\"Sword of Honour\",\"price\":12.99},{\"category\":\"fiction\",\"author\":\"Herman Melville\",\"title\":\"Moby Dick\",\"isbn\":\"0-553-21311-3\",\"price\":8.99},{\"category\":\"fiction\",\"author\":\"J. R. R. Tolkien\",\"title\":\"The Lord of the Rings\",\"isbn\":\"0-395-19395-8\",\"price\":22.99}],\"bicycle\":{\"color\":\"red\",\"price\":19.95}},\"expensive\":10}";

        //1、  JsonPath 最简单最直接的方法是通过静态读取 API ; 但每次读取都会重新解析JSON文档
        List<String> authors = JsonPath.read(jsonString, "$.store.book[*].author");
        System.out.println("获取json中store下book下的所有author值" + authors);

        //2、 解析整个JSON文档
        Object jsonDocument = Configuration.defaultConfiguration().jsonProvider().parse(jsonString);
        String oneAuthor = JsonPath.read(jsonDocument, "$.store.book[0].author");
        System.out.println("第一个作者" + oneAuthor);
        Integer booksLength = JsonPath.read(jsonDocument, "$..book.length()");
        System.out.println("获取json中book数组的长度" + booksLength);

        //3、 流式API
        ReadContext fluentDocument = JsonPath.parse(jsonString);
        List<String> authorsOfBooksWithISBN = fluentDocument.read("$.store.book[?(@.isbn)].author");
        System.out.println("获取json中book数组中包含isbn的author值" + authorsOfBooksWithISBN);

        List<Map<String, Object>> expensiveBooks = JsonPath
                .using(Configuration.defaultConfiguration())
                .parse(jsonString)
                .read("$.store.book[?(@.price > 10)]", List.class);
        System.out.println("查询价格大于10的书籍信息" + expensiveBooks);


        // 返回数据类型格式
        String json = "{\"date_as_long\" : 1411455611975}";
        Date date = JsonPath.parse(json).read("$['date_as_long']", Date.class);
        System.out.println("返回数据转换时间格式: " + date);

        Book book = JsonPath.parse(jsonString).read("$.store.book[0]", Book.class);
        System.out.println("返回一个Ban: " + book);

        /*转换错误 未解决
        TypeRef<List<Book>> typeRef = new TypeRef<List<Book>>(){};
        List<Book> bookList = JsonPath.parse(jsonString).read("$.store.book[?(@.price < 10)]", typeRef);
        System.out.println("返回List 泛型集合" + bookList);*/


    }

    @Data
    static class Book {
        private String category;
        private String author;
        private String title;
        private String isbn;
        private float price;
    }
}
